package com.musiklegal;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class FriendParser extends DefaultHandler
{
	static class FriendContainer
	{
		FriendContainer()
		{
			songlist = new ArrayList<DataSong>();
		}
		
		String username,fullname,picture,status,genre,fbid;
		List<DataSong> songlist;
	}
	DataSong d;
	FriendContainer packer;
	
	boolean username,fullname,picture,status,genre,fbid,entry;
	StringBuffer data;
	private boolean in_song;
	private boolean in_artist;
	private boolean in_idring;
	private boolean in_api;
	private boolean in_album;
	private boolean in_alname;
	private boolean in_comp;
	private boolean in_pub;
	private boolean in_genre;
	private boolean in_label;
	private boolean in_albumid;
	private boolean artist_id;
	private boolean preview;
	private boolean rating;
	private boolean origin;
	
	
	public void startDocument() throws SAXException 
	{
		data= new StringBuffer();
		packer = new FriendContainer();
	}
	
	@Override
	public void endDocument() throws SAXException 
	{
		
	}

	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException 
	{
		if(localName.compareToIgnoreCase("username")==0)
		{
			username = true;
		}
		else if(localName.compareToIgnoreCase("fullname")==0)
		{
			fullname = true;
		}
		else if(localName.compareToIgnoreCase("picture")==0)
		{
			picture = true;
		}
		else if(localName.compareToIgnoreCase("status")==0)
		{
			status = true;
		}
		else if(localName.compareToIgnoreCase("genre")==0)
		{
			genre = true;
		}
		else if(localName.compareToIgnoreCase("fbid")==0)
		{
			fbid = true;
		}
		else if(localName.compareToIgnoreCase("entry")==0)
		{
			entry = true;
			d= new DataSong();
		}
		else if (localName.equals("song")&&entry) {
			this.in_song = true;	
		}else if (localName.equals("artist")&&entry) {
				this.in_artist = true;	
		}else if (localName.equals("id_ring")&&entry) {
				this.in_idring = true;	
		}
		else if (localName.equals("merchant_api")&&entry) {
			this.in_api = true;	
		}else if (localName.equals("album_art")&&entry) {
				this.in_album = true;	
		}
		else if(localName.equalsIgnoreCase("album_id")&&entry)
		{
			 in_albumid = true;
		}
		else if (localName.equals("album_name")&&entry) {
				this.in_alname = true;	
		}
		else if (localName.equals("composer")&&entry) {
			this.in_comp = true;	
		}else if (localName.equals("publisher")&&entry) {
			this.in_pub = true;	
		}else if (localName.equals("genre")&&entry) {
				this.in_genre = true;	
		}else if (localName.equals("label")&&entry) {
				this.in_label = true;	
		}
		else if(localName.compareToIgnoreCase("artist_id")==0&&entry)
		{
			
			artist_id=true;
		}
		else if (localName.equals("preview")&&entry) 
		{
			
			
				this.preview = true;	
		}
		else if (localName.equals("rating")&&entry) 
		{
			
			
				this.rating = true;	
		}
		else if (localName.equals("origin")&&entry) 
		{
			//d. = data.toString();
			data = new StringBuffer();
			this.origin = false;	
		}
		
	}
	
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if(localName.compareToIgnoreCase("username")==0&&username)
		{
			packer.username = data.toString();
			data = new StringBuffer();
			username = false;
			
		}
		else if(localName.compareToIgnoreCase("fullname")==0&&fullname)
		{
			packer.fullname = data.toString();
			data = new StringBuffer();
			fullname = false;
		}
		else if(localName.compareToIgnoreCase("picture")==0&&picture)
		{
			packer.picture = data.toString();
			data = new StringBuffer();
			picture = false;
		}
		else if(localName.compareToIgnoreCase("status")==0&&status)
		{
			packer.status = data.toString();
			data = new StringBuffer();
			status = false;
		}
		else if(localName.compareToIgnoreCase("artist_id")==0&&artist_id)
		{
			d.artist_id = data.toString();
			data = new StringBuffer();
			artist_id=false;
		}
		else if(localName.compareToIgnoreCase("genre")==0&&genre)
		{
			packer.genre = data.toString();
			data = new StringBuffer();
			genre = false;
		}
		else if(localName.compareToIgnoreCase("fbid")==0&&fbid)
		{
			packer.fbid = data.toString();
			data = new StringBuffer();
			fbid = false;
		}
		if(localName.compareToIgnoreCase("entry")==0&&entry)
		{
			if(d.albid==null)
			{
				String[] t = d.album.split("/");
				d.albid = t[t.length-1];
			}
			d.genre = packer.genre;
			packer.songlist.add(d);
			
			d=null;
			entry=false;
		}
		else if (localName.equals("song")&&entry) 
		{
			d.song = data.toString();
			data = new StringBuffer();
			this.in_song = false;	
		}else if (localName.equals("artist")&&entry) 
		{
			
			d.artist = data.toString();
			data = new StringBuffer();
			this.in_artist = false;	
		}else if (localName.equals("id_ring")&&entry)
		{
			d.idring = data.toString();
			data = new StringBuffer();
				this.in_idring = false;	
		}
		else if (localName.equals("merchant_api")&&entry) 
		{
			d.api = data.toString();
			data = new StringBuffer();
				this.in_api = false;	
		}
		else if (localName.equals("album_art")&&entry) 
		{
			
			d.album = data.toString();
			data = new StringBuffer();
				this.in_album = false;	
		}
		else if (localName.equals("preview")&&entry) 
		{
			
			d.preview = data.toString();
			data = new StringBuffer();
				this.preview = false;	
		}
		else if(localName.equalsIgnoreCase("album_id")&&entry)
		{
			d.albid = data.toString();
			data = new StringBuffer();
			 in_albumid = false;
		}
		else if (localName.equals("album_name")&&entry)
		{
			d.alname = data.toString();
			data = new StringBuffer();
				this.in_alname = false;	
		}
		else if (localName.equals("composer")&&entry) 
		{
			d.composer = data.toString();
			data = new StringBuffer();
				this.in_comp = false;	
		}
		else if (localName.equals("publisher")&&entry) 
		{
			d.pub = data.toString();
			data = new StringBuffer();
			this.in_pub = false;
		}
		else if (localName.equals("genre")&&entry) {
			d.genre= data.toString();
			data = new StringBuffer();
				this.in_genre = false;	
				
		}
		else if (localName.equals("label")&&entry) 
		{
			
			d.label = data.toString();
			data = new StringBuffer();
			this.in_label = false;	
		}
		else if (localName.equals("rating")&&entry) 
		{
			d.rating = data.toString();
			data = new StringBuffer();
			this.rating = false;	
		}
		else if (localName.equals("origin")&&entry) 
		{
			//d. = data.toString();
			data = new StringBuffer();
			this.origin = false;	
		}
		
		
	}
	
	/** Gets be called on the following structure: 
	 * <tag>characters</tag> */
	@Override
    public void characters(char ch[], int start, int length) {
		
		data.append(new String(ch,start,length));
    }

}
